# main.py
from params import setup
from kgc import kgc_partial_keygen
from user import user_full_key
from sign import sign_message, verify_signature
from aggregate import aggregate_signatures, verify_aggregate
from cost import measure_computation_cost, measure_communication_cost, benchmark


def main(n: int = 100):
	"""主演示流程：模拟 n 辆车注册、签名并由 RSU 聚合验证。"""
	curve, hash_func = setup()
	# 模拟用户注册与密钥生成
	users = []  # 列表 of dict {id, partial, sk, pk}
	for i in range(n):
		uid = f"vehicle-{i}".encode("utf-8")
		partial = kgc_partial_keygen(uid)
		sk, pk = user_full_key(partial)
		users.append({"id": uid, "partial": partial, "sk": sk, "pk": pk})

	# 每辆车签一条消息
	messages = [b"speed=60" for _ in range(n)]
	signatures = [sign_message(u["sk"], m) for u, m in zip(users, messages)]

	# RSU 聚合签名
	agg_sig = aggregate_signatures(signatures)

	# 验证聚合签名（示例：重用 individual signatures 来验证）
	ok = verify_aggregate([u["pk"] for u in users], messages, agg_sig, signatures=signatures)

	# 计算开销示例
	comp_ms = measure_computation_cost(lambda: verify_aggregate([u["pk"] for u in users], messages, agg_sig, signatures=signatures), repeat=10)
	comm_bytes = measure_communication_cost(signatures)

	print(f"n={n} users, aggregate_verified={ok}")
	print(f"verify_aggregate avg time: {comp_ms:.2f} ms (repeat=10)")
	print(f"signatures total bytes (est): {comm_bytes} bytes")


if __name__ == "__main__":
	main(100)

